<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=293834
-->
<head>
  <title>Test for Bug 293834</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=293834">Mozilla Bug 293834</a>
<p id="display">

</p>
<pre id="results"></pre>
<div id="content" style="display: none">
    <iframe src="bug293834_form.html" id="source"></iframe>
    <br>
    <iframe id="dest"></iframe>
</div>
<pre id="test">
<script class="testbody" type="text/javascript">
/** Test for Bug 293834 **/

var textareas = ["a-textbox", "a-prefilled-textbox"];
var textboxes = ["a-text", "a-prefilled-text"];

function fillform(doc) {
    for (var i in textareas) {
        doc.getElementById(textareas[i]).textContent += "form state";
    }
    for (var i in textboxes) {
        doc.getElementById(textboxes[i]).value += "form state";
    }
    doc.getElementById('a-checkbox').checked = true;
    doc.getElementById("radioa").checked = true;
    doc.getElementById("aselect").selectedIndex = 0;
}

function checkform(doc) {
    for (var i in textareas) {
        var textContent = doc.getElementById(textareas[i]).textContent;
        ok(/form\s+state/m.test(textContent),
            "Modified textarea "+textareas[i]+" form state not preserved!");
    }
    for (var i in textboxes) {
        var value = doc.getElementById(textboxes[i]).value;
        ok(/form\s+state/m.test(value),
            "Modified textbox "+textboxes[i]+" form state not preserved!");
    }
    ok(doc.getElementById('a-checkbox').checked,
        "Modified checkbox checked state not preserved!");
    ok(doc.getElementById("radioa").checked,
        "Modified radio checked state not preserved!");
    ok(doc.getElementById("aselect").selectedIndex == 0,
        "Modified select selected index not preserved");
}

const Cc = SpecialPowers.Cc;
const Ci = SpecialPowers.Ci;

function getTempDir() {
    return Cc["@mozilla.org/file/directory_service;1"]
            .getService(Ci.nsIProperties)
            .get("TmpD", Ci.nsILocalFile);
}

function getFileContents(aFile) {
    const PR_RDONLY = 0x01;
    var fileStream = Cc["@mozilla.org/network/file-input-stream;1"]
                        .createInstance(Ci.nsIFileInputStream);
    fileStream.init(aFile, PR_RDONLY, 0400,
                    Ci.nsIFileInputStream.DELETE_ON_CLOSE
                  | Ci.nsIFileInputStream.CLOSE_ON_EOF);
    var inputStream = Cc["@mozilla.org/scriptableinputstream;1"]
                        .createInstance(Ci.nsIScriptableInputStream);
    inputStream.init(fileStream);
    var data = "";
    do {
        var str = inputStream.read(inputStream.available());
        data += str;
    } while(str.length > 0);

    return data;
}

function persistDocument(aDoc) {
    const nsIWBP = Ci.nsIWebBrowserPersist;
    const persistFlags =
                  nsIWBP.PERSIST_FLAGS_REPLACE_EXISTING_FILES
                | nsIWBP.PERSIST_FLAGS_FROM_CACHE
                | nsIWBP.PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION
    const encodingFlags =
                  nsIWBP.ENCODE_FLAGS_ENCODE_BASIC_ENTITIES;

    var ioService = Cc["@mozilla.org/network/io-service;1"]
                    .getService(Ci.nsIIOService);


    var file = getTempDir();
    file.append("bug293834-serialized.html");

    var persist = Cc["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"]
                    .createInstance(Ci.nsIWebBrowserPersist);
    persist.progressListener = null;
    persist.persistFlags = persistFlags;
    const kWrapColumn = 80;
    var folder = getTempDir();
    folder.append("bug293834-serialized");
    persist.saveDocument(aDoc, ioService.newFileURI(file),
                         folder,
                         aDoc.contentType,
                         encodingFlags, kWrapColumn);
    return getFileContents(file);
}

SimpleTest.waitForExplicitFinish();

addLoadEvent(function() {
    var srcDoc = document.getElementById('source').contentDocument;
    fillform(srcDoc);
    checkform(srcDoc);
    var serializedString = persistDocument(srcDoc);

    // We can't access file:/// URLs directly for security reasons,
    // so we have to parse the serialized content string indirectly
    var targetDoc = document.getElementById('dest').contentDocument;
    targetDoc.write(serializedString);

    checkform(targetDoc);
    SimpleTest.finish();
});
</script>
</pre>
</body>
</html>
